﻿using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	/// <summary>
	/// 需要进行属性值计算的节点渲染对象基类
	/// </summary>
	public class ExpressionValueHandler : ValueTypeHandler
	{
		/// <summary>
		/// 获取字段名称
		/// </summary>
		protected virtual string FieldName
		{
			get
			{
				return null;
			}
		}

		/// <summary>
		/// 获取点域、属性表达式的值
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="rootParamObject"></param>
		/// <param name="parentParamObject"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		protected virtual object GetExpressionValue ( string fieldName, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var paramObject = this.GetParamObject ( rootParamObject, parentParamObject, context );
			return this.GetExpressionValue ( paramObject, fieldName, rootParamObject, parentParamObject, context );
		}

		protected virtual object GetExpressionValue ( object paramObject, string fieldName, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			if ( fieldName.IsNullOrEmpty ( ) )
			{
				return parentParamObject;
			}
			if ( fieldName == Consts.VALUE_PREFIX )
			{
				return parentParamObject;
			}
			return ExpressionUtils.GetFieldValue
			(
				fieldName
				, rootParamObject
				, parentParamObject
				, context
				, ( ) => { return paramObject.GetValue ( fieldName ); }
			);
		}
		
		/// <summary>
		/// 获取字段、属性的值
		/// </summary>
		/// <param name="rootParamObject"></param>
		/// <param name="parentParamObject"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public virtual object GetFieldValue ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			return this.GetFieldValue ( this.GetFieldName ( rootParamObject, parentParamObject, context ), rootParamObject, parentParamObject, context );
		}

		/// <summary>
		/// 获取字段、属性的值
		/// </summary>
		/// <param name="filedName"></param>
		/// <param name="rootParamObject"></param>
		/// <param name="parentParamObject"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public virtual object GetFieldValue ( string filedName, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			if ( filedName.IsNullOrEmpty ( ) )
			{
				filedName = this.GetFieldName ( rootParamObject, parentParamObject, context );
			}
			var value = this.GetExpressionValue
			(
				filedName
				, rootParamObject
				, parentParamObject
				, context
			);
			return this.GetConvertedValue ( value );
		}

		/// <summary>
		/// 获取字段、属性名称
		/// </summary>
		/// <param name="rootParamObject"></param>
		/// <param name="parentParamObject"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public virtual string GetFieldName ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			return ExpressionUtils.GetFieldName ( this.FieldName, rootParamObject, parentParamObject, context );
		}

		/// <summary>
		/// 获取字段、属性名称
		/// </summary>
		/// <param name="rootParamObject"></param>
		/// <param name="parentParamObject"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public virtual string GetFieldName ( string fieldName, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			return ExpressionUtils.GetFieldName ( fieldName, rootParamObject, parentParamObject, context );
		}
	}
}
